package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.khyr.Vo.RevenueAndExpenditureStatisticsVo;
import com.khyr.dao.BackfeeDao;
import com.khyr.dao.ChargeDao;
import com.khyr.entity.Backfee;
import com.khyr.entity.Charge;
import com.khyr.service.RevenueAndExpenditureStatisticsService;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：Zhang
 * @date ：Created in 2025/3/11 15:03
 * @description：收支统计的服务层，用于根据时间查询
 * @modified By：
 * @version: 1.0
 */
@Service
public class RevenueAndExpenditureStatisticsServiceImpl implements RevenueAndExpenditureStatisticsService {
    @Autowired
    private BackfeeDao backfeeDao;
    @Autowired
    private ChargeDao chargeDao;
    /**
     * @create by: Zr
     * @description: 根据日期查询所有的收支信息，包括收费和退费金额以及收退费方式
     * @create time: 2025/3/12 14:47
     * @param time
     * @return com.khyr.vo.R<com.khyr.Vo.RevenueAndExpenditureStatisticsVo>
     */
    @Override
    public R<RevenueAndExpenditureStatisticsVo> getRevenue(String[] time) {
        RevenueAndExpenditureStatisticsVo rv = new RevenueAndExpenditureStatisticsVo();
        //收费和退费金额
        // fei[0]表示收费金额，fei[1]表示退费金额
        Long[] fei = {0L,0L};
        //收费方式统计
        // xiFei[0]表示现金，xiFei[1]表示微信，xiFei[2]表示支付宝，xiFei[3]表示退费现金，xiFei[4]表示退费微信，xiFei[5]表示退费支付宝
        Long[] xiFei = {0L,0L,0L,0L,0L,0L};
        if(time == null){
            //查询所有收费成功的金额
            QueryWrapper queryWrapperS = new QueryWrapper();
            queryWrapperS.eq("order_status","1");
            List<Charge> listS = chargeDao.selectList(queryWrapperS);
            for(Charge charge:listS) {
                if(charge != null){
                    fei[0] += charge.getOrderAmount();
                    if ("0".equals(charge.getPayType())) {
                        xiFei[0] += charge.getOrderAmount();
                    } else if ("1".equals(charge.getPayType())){
                        xiFei[1] += charge.getOrderAmount();
                    } else if ("2".equals(charge.getPayType())){
                        xiFei[2] += charge.getOrderAmount();
                    }
                }
            }
            //查询所有退费成功的金额
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("back_status","1");
            List<Backfee> list = backfeeDao.selectList(queryWrapper);
            for(Backfee backfee:list){
                fei[1] += backfee.getBackAmount();
                if("0".equals(backfee.getBackType())){
                    xiFei[3] += backfee.getBackAmount();
                }else if("1".equals(backfee.getBackType())){
                    xiFei[4] += backfee.getBackAmount();
                }else if("2".equals(backfee.getBackType())){
                    xiFei[5] += backfee.getBackAmount();
                }
            }
            // 将收费和退费金额封装到RevenueAndExpenditureStatisticsVo中
            rv.setRevenueAndExpenditure(fei);
            rv.setDetail(xiFei);
        }else{
            //根据传入的时间查询
            QueryWrapper queryWrapperS = new QueryWrapper();
            queryWrapperS.eq("order_status","1");
            queryWrapperS.between("update_time",time[0],time[1]);
            List<Charge> listS = chargeDao.selectList(queryWrapperS);
            //
            for(Charge charge:listS) {
                fei[0] += charge.getOrderAmount();
                if ("0".equals(charge.getPayType())) {
                    xiFei[0] += charge.getOrderAmount();
                } else if ("1".equals(charge.getPayType())){
                    xiFei[1] += charge.getOrderAmount();
                } else if ("2".equals(charge.getPayType())){}
            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("back_status","1");
            queryWrapper.between("update_time",time[0],time[1]);
            List<Backfee> list = backfeeDao.selectList(queryWrapper);
            for(Backfee backfee:list){
                fei[1] += backfee.getBackAmount();
                if("0".equals(backfee.getBackType())){
                    xiFei[3] += backfee.getBackAmount();
                }else if("1".equals(backfee.getBackType())){
                    xiFei[4] += backfee.getBackAmount();
                }else if("2".equals(backfee.getBackType())){
                   xiFei[5] += backfee.getBackAmount();
                }
            }
            rv.setRevenueAndExpenditure(fei);
            rv.setDetail(xiFei);
        }
        return new R<RevenueAndExpenditureStatisticsVo>(200,"查询成功",rv);
    }
}
